<?php

Class Login extends CI_Controller {

//constructor
    function __construct() {
        parent::__construct();
        $this->load->model('model_user');
    }

    function redirect() {
        if (isset($this->session->userdata['level'])) {
            if ($this->session->userdata['level'] == 'dosen') {
                redirect('dosen');
            } else if ($this->session->userdata['level'] == 'mahasiswa') {
                redirect('mahasiswa');
            } else {
                $this->load->view('login_view');
            }
        } else {
            $this->load->view('login_view');
        }
    }

//index for showing the login form
    function index() {
        $this->redirect();
    }

//this function will do the login process
    function proseslogin() {
        $this->load->model('model_user');

        if ($this->model_user->validate()) {
            if ($this->session->userdata['level'] == 'dosen') {
                redirect('dosen');
            } else if ($this->session->userdata['level'] == 'mahasiswa') {
                redirect('mahasiswa');
            }
        } else {
            $data['error'] = 'Anda tidak terdaftar.';
            $this->load->view('login_view', $data);
        }
    }

//to do logout process
    function logout() {
        $this->session->sess_destroy();
        $data['logout'] = 'You have been logged out.';
        $this->load->view('login_view', $data);
    }
    
        
    function goSetting(){
        $data['user'] = $this->model_user->user_creds();
        $data['main_content'] = 'setting';
        $this->load->view('includes/template', $data);
    }
    
    function profpicedit(){
        $this->model_user->edit_profpic();
        $this->goSetting();
    }
    
    function editpass(){
        echo $this->model_user->edit_pass();
    }

}

?>